//
//  NRWSeriseTableView.m
//  NRWMall
//
//  Created by 邢毅 on 2018/6/4.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWSeriesTableView.h"
#import "NRWBrandSeriesCell.h"
#import "NRWSeriesModel.h"


@interface NRWSeriesTableView()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) UITableView *tableView;

@property (assign, nonatomic) CGPoint startPoint;

@property (weak, nonatomic) UIView *superView;

@end

@implementation NRWSeriesTableView

- (void)setModel:(NRWSeriesModel *)model {
    
    _model = model;
    
    [self.tableView reloadData];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        self.layer.shadowOffset = CGSizeMake(-8, 0);
        self.layer.shadowOpacity = 0.1;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        [self setupUI];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
        [self addGestureRecognizer:pan];
    }
    return self;
}

- (void)setupUI {
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = DPView();
    self.tableView.frame = CGRectMake(SCREEN_WIDTH(), 0, SCREEN_WIDTH() * 0.74, SCREEN_HEIGHT() - getNavHeight());
    [self.tableView registerClass:[NRWBrandSeriesCell class] forCellReuseIdentifier:@"cell"];
    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self addSubview:self.tableView];
}

#pragma mark - publicMethod
- (void)showForView:(UIView *)view {
    
    self.isShow = YES;
    self.superView = view;
    
    [view addSubview:self];
    [view bringSubviewToFront:self];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.tableView.frame = CGRectMake(0, 0, self.viewWidth, self.viewHeight);
    }];
}

- (void)dismiss {
    
    [self dismissWithDuration:0.25];
}

- (void)dismissWithDuration:(NSTimeInterval)duration {
    
    [UIView animateWithDuration:duration animations:^{
        self.tableView.frame = CGRectMake(SCREEN_WIDTH(), 0, self.viewWidth, self.viewHeight);
    } completion:^(BOOL finished) {
        NSLog(@"%d", finished);
        [self removeFromSuperview];
        self.isShow = NO;
    }];
}

#pragma mark - privateMethod
- (void)panAction:(UIPanGestureRecognizer *)pan {
    
    CGPoint panPoint = [pan locationInView:self];
    
    if (pan.state == UIGestureRecognizerStateBegan) {
        self.startPoint = [pan locationInView:self];
    } else if (pan.state == UIGestureRecognizerStateChanged) {
        
        if (panPoint.x - self.startPoint.x < 0) {
            self.tableView.frame = CGRectMake(0, 0, self.viewWidth, self.viewHeight);
        } else if (panPoint.x - self.startPoint.x > 0 && panPoint.x < self.viewWidth) {
            self.tableView.frame = CGRectMake(panPoint.x - self.startPoint.x, 0, self.viewWidth, self.viewHeight);
        }
    } else if (pan.state == UIGestureRecognizerStateEnded) {
        
        if (panPoint.x - self.startPoint.x > self.viewWidth * 0.15) {
            
            NSTimeInterval duration = (1 - self.tableView.viewX / SCREEN_WIDTH() / 0.74) * 0.25;
            [self dismissWithDuration:duration];
        } else {
            
            [self showForView:self.superView];
        }
    }
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0 || section == 1) {
        return 1;
    }
    
    return self.model.result.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NRWBrandSeriesCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.isSeriesCell = YES;
    
    if (indexPath.section == 1) {
        
        cell.titleString = @"不限车系";
        cell.logoString = @"serise_all";
        return cell;
    } else if (indexPath.section == 0) {
        
        cell.isSeriesCell = NO;
        cell.titleString = self.brandName;
        cell.logoString = self.brandLogo;
        return cell;
    }
    
    cell.titleString = self.model.result[indexPath.row].seriesName;
    cell.didSelected = [self.model.result[indexPath.row].seriesId isEqualToString:self.selectId];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if ([self.delegate_series respondsToSelector:@selector(seriesTableView:didSelectedAtIndexPath:)]) {
        
        [self.delegate_series seriesTableView:self didSelectedAtIndexPath:indexPath];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 45.0f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *header = DPView().setBackgroundColor(backColor()).setFrame(CGRectMake(0, 0, SCREEN_WIDTH(), 20));
    
    UILabel *label = DPLabel().setTextColor(getColor(@"#333333")).setFont(getThemeFont(12)).setFrame(CGRectMake(17, 0, SCREEN_WIDTH() - 17, 20));
    
    if (section == 1) {
        
        label.text = @"不限车系";
    }
    
    header.addSubview(label);
    
    return header;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 0;
    }
    
    return 20.f;
}

@end
